package com.lgd.warehousemanagement.Utils;

import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.util.Log;
import android.widget.TimePicker;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @Author lgd
 * @Date 2024/6/28 17:04
 *   获取时间  工具类
 */
public class TimeUtils {
    private static final String TAG = "TimeUtils";

    // 获取当前日期时间（格式：yyyy-MM-dd HH:mm:ss）
    public static String getCurrentDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdf.format(new Date());
    }

    // 获取当前日期（格式：yyyy-MM-dd）
    public static String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return sdf.format(new Date());
    }
    //  格式：yyyy-MM-dd HH:mm:ss  转化为  格式：yyyy-MM-dd
    public static String dateConversion (String time) throws ParseException {
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTimeObj = dateTimeFormat.parse(time);
        String dateStr = dateFormat.format(dateTimeObj);
        return dateStr;
    }
    // 获取当前时间（格式：HH:mm:ss）
    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        return sdf.format(new Date());
    }

    // 获取当前年份
    public static int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    // 获取当前月份（1-12）
    public static int getCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1; // 注意月份是从0开始的，所以需要+1
    }

    // 获取当前日期（1-31）
    public static int getCurrentDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    // 获取当前小时（0-23）
    public static int getCurrentHour() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    // 获取当前分钟（0-59）
    public static int getCurrentMinute() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MINUTE);
    }

    // 获取当前秒数（0-59）
    public static int getCurrentSecond() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.SECOND);
    }

    public interface TimePickerCallback {
        void onTimePicked(Boolean b,String formattedDateTime);
    }
//      时间选择  小时 + 分钟
    public static void timeSelect(Context context, String startTime, TimePickerCallback callback) {
        final StringBuilder sb = new StringBuilder();
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);  // 小时
        int minute = calendar.get(Calendar.MINUTE);     // 分钟
        new TimePickerDialog(context, AlertDialog.THEME_HOLO_LIGHT, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int min) {
                sb.setLength(0);
                String formattedTime = formatTime(hourOfDay, min);
                Log.d(TAG, "时间选择时间 小时+分钟：" + formattedTime);
                SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date dateTimeObj = dateTimeFormat.parse(startTime);
                    String dateStr = dateFormat.format(dateTimeObj);
                    Log.d("TAG", "提取上班打卡年月日:" + dateStr);
                    sb.append(dateStr).append(" ").append(formattedTime).append(":00");
                    Log.d("TAG", "---拼接后的时间---" + sb.toString());
                    boolean b = getTimeCompareSize(startTime,sb.toString());  // 时间 对比
                    if (callback != null) {   //  回调接口
                        callback.onTimePicked(b,sb.toString());
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }, hour, minute, true).show();
    }
    /**
         TimeUtils.timeSelect(MainActivity.this, "2024-7-2 12:12:00", new TimeUtils.TimePickerCallback() {
            @Override
            public void onTimePicked(Boolean b, String formattedDateTime) {
                ToastUtils.successShowLong(getApplicationContext(),formattedDateTime+"--返回值---"+String.valueOf(b));
                Log.d(TAG, "---时间选择----"+formattedDateTime+"--时间判断--"+b);  // b = false 表示 时间大于 开始时间
            }
        });
     **/

    public static String formatTime(int hourOfDay, int minute) {    // 使用String.format来格式化时间
        return String.format(Locale.getDefault(), "%02d:%02d", hourOfDay, minute);
    }
    /**
     * 对比两个时间
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean getTimeCompareSize(String startTime, String endTime) {
        int i = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//年-月-日 时-分
        try {
            Date date1 = dateFormat.parse(startTime);//开始时间
            Date date2 = dateFormat.parse(endTime);//结束时间
            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间
            if (date2.getTime() < date1.getTime()) {
                i = 1;
            } else if (date2.getTime() == date1.getTime()) {
                i = 2;
            } else if (date2.getTime() > date1.getTime()) {
                //正常情况下的逻辑操作.
                i = 3;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (i == 1 || i == 2) {
            return true;
        } else {
            return false;   // 结束时间 > 开始时间
        }
    }

    // 计算 两个时间 差 ( 分钟 )
    public static Long setTime (String start, String end){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date end_data = formatter.parse(end);//结束时间
            Date start_data = formatter.parse(start);//开始时间
            // 这样得到的差值是微秒级别
            long diff = end_data.getTime() - start_data.getTime();
//            long days = diff / (1000 * 60 * 60 * 24);
//            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
//            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
//            Log.e("-=-", "剩余: " + days + "天" + hours + "小时" + minutes + "分");
            long minutes = diff / (1000 * 60);
            Log.e("-=-", "相差: " + minutes + " 分钟");
            return minutes;
        } catch (Exception e) {
            Log.e("-=-", e.getMessage());
            return 0L;
        }

    }
    public static class TimeInfo{
        private Double time;
        private String timeStr;

        public TimeInfo() {
        }
        public TimeInfo(Double time, String timeStr) {
            this.time = time;
            this.timeStr = timeStr;
        }

        public Double getTime() {
            return time;
        }

        public void setTime(Double time) {
            this.time = time;
        }

        public String getTimeStr() {
            return timeStr;
        }

        public void setTimeStr(String timeStr) {
            this.timeStr = timeStr;
        }
    }
    // 计算两个时间相差多少小时
    public static TimeInfo getTime(String startTime ,String endTime) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = dateFormat.parse(startTime);
        Date endDate = dateFormat.parse(endTime);
        Integer datePoor = getDatePoor(startDate,endDate);
        BigDecimal b = BigDecimal.valueOf((double) datePoor / 60);
        Double hour = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();//第一个参数是保留小数的位数
//        Log.d(TAG, "  结束的时间...."+ endTime);
        return new TimeInfo(hour,endTime);
    }
    public static Integer getDatePoor(Date startDate, Date endDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        Long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        Long mymint = diff/1000/60;
        int intValue = mymint.intValue();
        return intValue;
    }

}
